//
//  ZJManageAddressController.m
//  ZiJinLian
//
//  Created by lc on 2017/3/22.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "ZJManageAddressController.h"
#import "ZJAddAddressViewController.h"
@interface ZJManageAddressController ()

@end

@implementation ZJManageAddressController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
     self.navigationController.navigationBar.hidden=NO;
}

-(void)createView{
    [self initTitle:MyString(@"地址管理")];
    _addBtn =[ZJUITools getBottomBtn:MyString(@"新增地址") image:@"icon_plus"];
    self.tableView=[[UITableView alloc]init];
    [self.view addSubview:_tableView];
    [self.view addSubview:_addBtn];
    [_addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view.mas_bottom);
        make.height.mas_equalTo(50);
        make.left.equalTo(self.view.mas_left);
        make.width.mas_equalTo(MainW);
    }];
    self.tableView.backgroundColor=RGB(246, 246, 246);
   self.tableView.frame=CGRectMake(0, 0,MainW, MainH-64-50);
      
    
    
    
}


-(void)doOpeartion{
    _tableView.delegate=self;
    _tableView.dataSource =self;
    _tableView.sectionFooterHeight=10;
    [_addBtn addTarget:self action:@selector(addAddress) forControlEvents:UIControlEventTouchUpInside];
}


-(void)addAddress{
    ZJAddAddressViewController* controller =[[ZJAddAddressViewController alloc]init];
    __weak typeof(self) mySelf = self;
    controller.addData=^(ZJAdressModel* model){
        if (model.isDefault==1) {
            for (int i=0; i<_data.count; i++) {
                ZJAdressModel* model =_data[i];
                model.isDefault=0;
            }
        }
        [_data addObject:model];
        [_tableView reloadData];
        if (mySelf.reflesh) {
            mySelf.reflesh(nil);
        }
        [mySelf.navigationController popViewControllerAnimated:YES];
    };
    [self.navigationController pushViewController:controller animated:YES];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 1;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

   
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString* ID=@"cell";
    ZJAddressManageCell* cell= [_tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell=[[ZJAddressManageCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    [cell setupCell:_data[indexPath.section]];
    [self cellOpaertion:cell index:indexPath.section];
    return cell;
}

-(void)cellOpaertion:(ZJAddressManageCell*)cell index:(NSInteger) index{
    [cell.setDeafulBtn addTarget:self action:@selector(setDeaful:) forControlEvents:UIControlEventTouchUpInside];
    [cell.editBtn addTarget:self action:@selector(edit:) forControlEvents:UIControlEventTouchUpInside];
    [cell.deleteBtn addTarget:self action:@selector(delete:) forControlEvents:UIControlEventTouchUpInside];
    cell.setDeafulBtn.tag=1000+index;
    cell.editBtn.tag=1000+index;
    cell.deleteBtn.tag=1000+index;
}


-(void)edit:(UIButton*)btn{
    ZJAddAddressViewController* controller =[[ZJAddAddressViewController alloc]init];
    controller.isEdit=YES;
    controller.model=_data[btn.tag-1000];
      __weak typeof(self) mySelf = self;
    controller.addData=^(ZJAdressModel* model){
        if (mySelf.reflesh) {
            mySelf.reflesh(nil);
        }
        [mySelf.navigationController popViewControllerAnimated:YES];
    };
    [self.navigationController pushViewController:controller animated:YES];
}


-(void)delete:(UIButton*)btn{
    ZJAdressModel* model=_data[btn.tag-1000];
    if (model.isDefault==1) {
        [self showMessage:MyString(@"默认地址无法删除")];
        return;
    }
//    if (_data.count==1) {
//        [self showMessage:MyString(@"至少保留一个地址")];
//        return;
//    }
    
    NSMutableDictionary* dic =[[NSMutableDictionary alloc]init];
    [dic setObject:model.addressId forKey:@"addressId"];
    [dic setObject:[ZJUserDefaultsTools getCurrentUserID] forKey:@"userId"];
   [self doHttpRequestWithApi:API_REMOVE_ADDRESS method:DELETE dic:dic successBlock:^(NSDictionary *dic) {
       [_data removeObject:model];
       if (self.reflesh) {
           self.reflesh(nil);
       }
       [_tableView reloadData];
   }];
}


-(void)setDeaful:(UIButton*)btn{
    ZJAdressModel* model=_data[btn.tag-1000];
    if (model.isDefault==1) {
        return;
    }
    NSMutableDictionary* dic =[[NSMutableDictionary alloc]init];
    [dic setObject:model.addressId forKey:@"addressId"];
    [dic setObject:[ZJUserDefaultsTools getCurrentUserID] forKey:@"userId"];
    [self doHttpRequestWithApi:API_SETDEFAULT_ADDRESS method:PUT dic:dic successBlock:^(NSDictionary *dic) {
        for (int i=0; i<_data.count; i++) {
            ZJAdressModel* model =_data[i];
            model.isDefault=0;
        };
        model.isDefault=1;
        if (self.reflesh) {
            self.reflesh(nil);
        }
        [_tableView reloadData];
    }];

}




-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZJAdressModel* model =_data[indexPath.row];
    return 10+[model.consigneeName heightWithFont:FontAndStyle14 withinWidth:MainW]+10+[model.consigneeName heightWithFont:FontAndStyle14 withinWidth:MainW]+10+1+10+20+10;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 5;
}


-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [ZJUITools getCutlineCustom:5];
}



-(void)doRequest{


}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  
    return _data.count;
}

-(NSMutableArray *)data{
    if (!_data) {
        _data=[[NSMutableArray alloc]init];
    }
    return _data;
}
@end
